/*
 * Copyright (c) 2025 Li Auto Inc. and its affiliates
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef EDDS_RTPS_BUILTIN_DISCOVERY_PARTICIPANT_DS__DISCOVERYSERVERPDPENDPOINTS_HPP_
#define EDDS_RTPS_BUILTIN_DISCOVERY_PARTICIPANT_DS__DISCOVERYSERVERPDPENDPOINTS_HPP_

#include <memory>

#include <edds/rtps/builtin/data/BuiltinEndpoints.hpp>

#include <edds/rtps/builtin/BuiltinReader.hpp>
#include <edds/rtps/builtin/BuiltinWriter.hpp>
#include <discovery/participant/PDPEndpoints.hpp>
#include <history/ITopicPayloadPool.h>
#include <edds/rtps/reader/StatefulReader.h>
#include <edds/rtps/writer/StatefulWriter.h>
#include <edds/rtps/reader/StatelessReader.h>
#include <edds/rtps/writer/StatelessWriter.h>

namespace evbs {
namespace ertps {
namespace rtps {

/**
 * Container for the builtin endpoints of non-secure PDPClient and PDPServer
 */
struct DiscoveryServerPDPEndpoints : public PDPEndpoints {
    ~DiscoveryServerPDPEndpoints() override = default;

    // BuiltinEndpointSet_t builtin_endpoints() const override
    uint32_t builtin_endpoints() const override {
        return DISC_BUILTIN_ENDPOINT_PARTICIPANT_ANNOUNCER | DISC_BUILTIN_ENDPOINT_PARTICIPANT_DETECTOR;
    }

    const std::unique_ptr<vbs::common::ReaderListener>& main_listener() const override { return reader.listener_; }

    bool enable_pdp_readers(RTPSParticipantImpl* participant) override {
        bool ret = false;
        ret = participant->enableReader(reader.reader_);
        if (!ret) {
            return ret;
        }
        if (participant->getRTPSParticipantAttributes().builtin.discovery_config.is_compatible &&
            compatible_reader.reader_) {
            ret = participant->enableReader(compatible_reader.reader_);
            if (!ret) {
                elogError(RTPS_PDP_LISTENER, RetCode_t::RETCODE_NOT_ENABLED, "The compatible Reader cannot be enabled");
            }
        }
        return ret;
    }

    void disable_pdp_readers(RTPSParticipantImpl* participant) override {
        participant->disableReader(reader.reader_);
        if (participant->getRTPSParticipantAttributes().builtin.discovery_config.is_compatible &&
            compatible_reader.reader_) {
            participant->disableReader(compatible_reader.reader_);
        }
    }

    void delete_pdp_endpoints(RTPSParticipantImpl* participant) override {
        participant->deleteUserEndpoint(writer.writer_->getGuid());
        participant->deleteUserEndpoint(reader.reader_->getGuid());

        if (participant->getRTPSParticipantAttributes().builtin.discovery_config.is_compatible &&
            compatible_reader.reader_) {
            participant->deleteUserEndpoint(compatible_reader.reader_->getGuid());
        }
    }

    void remove_from_pdp_reader_history(const InstanceHandle_t& remote_participant) override {
        reader.remove_from_history(remote_participant);
    }

    void remove_from_pdp_reader_history(CacheChange_t* change) override { reader.history_->remove_change(change); }

    //! get is_send_multi
    bool getSendMulti() const override { return writer.writer_->getSendMulti(); }

    //! set is_send_multi
    void setSendMulti(bool send_multi) override { return writer.writer_->setSendMulti(send_multi); }

    //! Builtin Simple PDP reader
    BuiltinReader<StatefulReader> reader;

    //! Builtin Simple PDP writer
    BuiltinWriter<StatefulWriter> writer;

    //! Builtin Compatible PDP reader
    BuiltinReader<StatelessReader> compatible_reader;
};

}  // namespace rtps
}  // namespace ertps
}  // namespace evbs

#endif  // EDDS_RTPS_BUILTIN_DISCOVERY_PARTICIPANT_DS__DISCOVERYSERVERPDPENDPOINTS_HPP_
